package repository

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"go-fox-edge-kernel-manager/repository/cloudRemoteService"
	"net/http"
)

type repoLocalCompSync struct {
}

func (e *repoLocalCompSync) SyncEntity(compId int64) (map[string]interface{}, error) {
	data := commEntityManager.GetEntityById(edgeEntity.Type.RepoCompEntity.Type(), compId)
	if data == nil {
		return nil, errors.New("实体不存在")
	}
	entity := data.(*edgeEntity.RepoCompEntity)

	if entity.CompRepo == "local" && entity.CompType == "jar-decoder" {
		return e.syncJarDecoderEntity(entity)
	}
	if entity.CompRepo == "local" && entity.CompType == "jsp-decoder" {
		return e.syncJspDecoderEntity(entity)
	}
	if entity.CompRepo == "local" && entity.CompType == "jsn-decoder" {
		return e.syncJsnDecoderEntity(entity)
	}
	if entity.CompRepo == "local" && entity.CompType == "device-template" {
		return e.syncDevTemplateEntity(entity)
	}
	if entity.CompRepo == "local" && entity.CompType == "iot-template" {
		return e.syncIotTemplateEntity(entity)
	}

	return nil, errors.New("该组件类型，不支持从云端同步！")
}

func (e *repoLocalCompSync) syncJarDecoderEntity(entity *edgeEntity.RepoCompEntity) (map[string]interface{}, error) {
	compParam := entity.CompParam

	modelName := compParam["modelName"]
	if Method.HasEmpty(modelName) {
		return nil, errors.New("缺少参数： modelName")
	}

	body := make(map[string]interface{})
	body["modelName"] = modelName
	body["modelType"] = "decoder"
	body["modelVersion"] = "v1"

	// 获得云端的信息
	respond, err := cloudRemoteService.ExecutePost("/manager/repository/component/groupName", body)
	if err != nil {
		return nil, err
	}
	code := respond["code"]
	data := respond["data"]

	if http.StatusOK != Number.MakeInt(code) {
		return nil, errors.New("从云端查询信息失败！" + Json.BuildJson(respond))
	}
	if data == nil {
		return nil, errors.New("云端没有这个组件，请先去云端仓库为本账号,归属的群组，注册这个组件:" + modelName.(string))
	}

	// 找到了云端的组件信息
	mp := data.(map[string]interface{})

	// 克隆一个副本，防止修改影响到了原本
	clone := edgeEntity.Type.RepoCompEntity.Instance()
	Json.Clone(entity, clone)

	clone.CompParam["groupName"] = mp["groupName"]
	clone.CompParam["compId"] = mp["id"]

	// 保存数据
	commEntityManager.UpdateEntity(clone)

	return respond, nil
}

func (e *repoLocalCompSync) syncJspDecoderEntity(entity *edgeEntity.RepoCompEntity) (map[string]interface{}, error) {
	compParam := entity.CompParam

	deviceType := compParam["deviceType"]
	manufacturer := compParam["manufacturer"]

	if Method.HasEmpty(deviceType, manufacturer) {
		return nil, errors.New("缺少参数： deviceType, manufacturer")
	}

	body := make(map[string]interface{})
	body["manufacturer"] = manufacturer
	body["deviceType"] = deviceType

	// 获得云端的信息
	respond, err := cloudRemoteService.ExecutePost("/manager/repository/component/script/title/list", body)
	if err != nil {
		return nil, err
	}
	code := respond["code"]
	data := respond["data"]

	if http.StatusOK != Number.MakeInt(code) || data == nil {
		return nil, errors.New("从云端查询信息失败！" + Json.BuildJson(respond))
	}

	// 如果空列表：云端没有这个组件
	list := data.([]interface{})
	if len(list) == 0 {
		return nil, errors.New("云端没有这个组件，请先去云端仓库为本账号,归属的群组，注册这个组件:" + deviceType.(string))
	}

	// 找到了云端的组件信息
	elm := (list[0]).(map[string]interface{})

	// 克隆一个副本，防止修改影响到了原本
	clone := edgeEntity.Type.RepoCompEntity.Instance()
	Json.Clone(entity, clone)

	clone.CompParam["groupName"] = elm["groupName"]
	clone.CompParam["compId"] = elm["id"]

	// 保存数据
	commEntityManager.UpdateEntity(clone)

	return respond, nil
}

func (e *repoLocalCompSync) syncJsnDecoderEntity(entity *edgeEntity.RepoCompEntity) (map[string]interface{}, error) {
	compParam := entity.CompParam

	deviceType := compParam["deviceType"]
	manufacturer := compParam["manufacturer"]
	if Method.HasEmpty(deviceType, manufacturer) {
		return nil, errors.New("缺少参数： deviceType, manufacturer")
	}

	body := make(map[string]interface{})
	body["manufacturer"] = manufacturer
	body["deviceType"] = deviceType

	// 获得云端的信息
	respond, err := cloudRemoteService.ExecutePost("/manager/repository/component/model/title/list", body)
	if err != nil {
		return nil, err
	}
	code := respond["code"]
	data := respond["data"]

	if http.StatusOK != Number.MakeInt(code) || data == nil {
		return nil, errors.New("从云端查询信息失败！" + Json.BuildJson(respond))
	}

	// 如果空列表：云端没有这个组件
	list := data.([]interface{})
	if len(list) == 0 {
		return nil, errors.New("云端没有这个组件，请先去云端仓库为本账号,归属的群组，注册这个组件:" + deviceType.(string))
	}

	// 找到了云端的组件信息
	mp := list[0].(map[string]interface{})

	// 克隆一个副本，防止修改影响到了原本
	clone := edgeEntity.Type.RepoCompEntity.Instance()
	Json.Clone(entity, clone)

	clone.CompParam["groupName"] = mp["groupName"]
	clone.CompParam["compId"] = mp["id"]

	// 保存数据
	commEntityManager.UpdateEntity(clone)

	return respond, nil
}

func (e *repoLocalCompSync) syncDevTemplateEntity(entity *edgeEntity.RepoCompEntity) (map[string]interface{}, error) {
	compParam := entity.CompParam

	deviceType := compParam["deviceType"]
	manufacturer := compParam["manufacturer"]
	subsetName := compParam["subsetName"]
	if Method.HasEmpty(deviceType, manufacturer, subsetName) {
		return nil, errors.New("缺少参数：deviceType, manufacturer, subsetName")
	}

	body := make(map[string]interface{})
	body["manufacturer"] = manufacturer
	body["deviceType"] = deviceType
	body["subsetName"] = subsetName

	// 获得云端的信息
	respond, err := cloudRemoteService.ExecutePost("/manager/repository/component/dev-template/title/list", body)
	if err != nil {
		return nil, err
	}
	code := respond["code"]
	data := respond["data"]

	if http.StatusOK != Number.MakeInt(code) || data == nil {
		return nil, errors.New("从云端查询信息失败！" + Json.BuildJson(respond))
	}

	// 如果空列表：云端没有这个组件
	list := data.([]interface{})
	if len(list) == 0 {
		return nil, errors.New("云端没有这个组件，请先去云端仓库为本账号,归属的群组，注册这个组件:" + deviceType.(string))
	}

	// 找到了云端的组件信息
	mp := list[0].(map[string]interface{})

	// 克隆一个副本，防止修改影响到了原本
	clone := edgeEntity.Type.RepoCompEntity.Instance()
	Json.Clone(entity, clone)

	clone.CompParam["groupName"] = mp["groupName"]
	clone.CompParam["compId"] = mp["id"]

	// 保存数据
	commEntityManager.UpdateEntity(clone)

	return respond, nil
}

func (e *repoLocalCompSync) syncIotTemplateEntity(entity *edgeEntity.RepoCompEntity) (map[string]interface{}, error) {
	compParam := entity.CompParam

	iotName := compParam["iotName"]
	subsetName := compParam["subsetName"]
	if Method.HasEmpty(iotName, subsetName) {
		return nil, errors.New("缺少参数：iotName, subsetName")
	}

	body := make(map[string]interface{})
	body["iotName"] = iotName
	body["subsetName"] = subsetName

	// 获得云端的信息
	respond, err := cloudRemoteService.ExecutePost("/manager/repository/component/iot-template/title/list", body)
	if err != nil {
		return nil, err
	}
	code := respond["code"]
	data := respond["data"]

	if http.StatusOK != Number.MakeInt(code) || data == nil {
		return nil, errors.New("从云端查询信息失败！" + Json.BuildJson(respond))
	}

	// 如果空列表：云端没有这个组件
	list := data.([]interface{})
	if len(list) == 0 {
		return nil, errors.New("云端没有这个组件，请先去云端仓库为本账号,归属的群组，注册这个组件:" + iotName.(string))
	}

	// 找到了云端的组件信息
	mp := list[0].(map[string]interface{})

	// 克隆一个副本，防止修改影响到了原本
	clone := edgeEntity.Type.RepoCompEntity.Instance()
	Json.Clone(entity, clone)

	clone.CompParam["groupName"] = mp["groupName"]
	clone.CompParam["compId"] = mp["id"]

	// 保存数据
	commEntityManager.UpdateEntity(clone)

	return respond, nil
}
